<?php

/*
 * Table/PlanningPeriodeTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use RHX\Model\Object\PlanningPeriode;

class PlanningPeriodeTable extends AbstractTableGateway {

    protected $table = 'rh_planning_periode';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new PlanningPeriode());
	$this->initialize();
    }

    public function getAll() {
	return $this->select();
    }

    public function get($id) {
	$row = $this->select(array('id' => $id))
		->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver #$id");

	return $row;
    }

    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

    public function save(PlanningPeriode $pl) {
	$data	 = array(
	    'periode_id'	 => $pl->periode_id,
	    'semaine_id'	 => $pl->semaine_id,
	    'template'	 => $pl->template,
	);
	$id	 = (int) $pl->id;
	if ($id == 0) {
	    $this->insert($data);

	    return $this->lastInsertValue;
	}
	elseif ($this->get($id)) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    throw new \Exception("Impossible de sélectionner #$id");
	}
    }

}

?>
